package com.zoo.diannaopeijian.controller.purchase;

import com.zoo.diannaopeijian.common.page.Page;
import com.zoo.diannaopeijian.controller.BaseController;
import com.zoo.diannaopeijian.domain.PurchaseItem;
import com.zoo.diannaopeijian.service.PurchaseItemService;
import com.zoo.diannaopeijian.service.PurchaseRecordService;
import com.zoo.diannaopeijian.service.PurchaseService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 入货控制器
 */
@Controller
@RequestMapping("purchase")
public class PurchaseController extends BaseController {
    @Resource
    private PurchaseService purchaseService;
    @Resource
    private PurchaseRecordService purchaseRecordService;
    @Resource
    private PurchaseItemService purchaseItemService;

    @GetMapping(value = "/pages/{pageName}")
    public String pageRoute(@PathVariable String pageName) {
        return "purchase/" + pageName;
    }

//    @RequestMapping("EntryPage")
//    public String EntryPageRoute() {
//        return "/entry/EntryPage";
//    }
//
//    @RequestMapping("ReEntryPage")
//    public String ReEntryPageRoute() {
//        return "/entry/ReEntryPage";
//    }
//
//    @RequestMapping("EntryRecordPage")
//    public String EntryRecordPageRoute() {
//        return "/entry/EntryRecordPage";
//    }
//
//    @RequestMapping("EntryItemPage")
//    public String EntryItemPageRoute() {
//        return "/entry/EntryItemPage";
//    }

    @PostMapping(value = "/purchaseRecord")
    public ResponseEntity<String> purchaseWares(@RequestBody List<PurchaseItem> purchaseItemList) {
        return ResponseEntity.ok(purchaseService.purchaseWares(getLoginId(), purchaseItemList) ? success : fail);
    }

    @PutMapping(value = "/purchaseRecord")
    public ResponseEntity<String> rePurchaseWares(@RequestBody List<PurchaseItem> purchaseItems) {
        Integer purchaseRecordId = purchaseItems.get(0).getPurRecId();

        if (purchaseItems.get(0).getWareId() == null) {
            purchaseItems.clear();
        }

        return ResponseEntity.ok(purchaseService.rePurchaseWares(purchaseItems, purchaseRecordId) ? success : fail);
    }

    @GetMapping(value = "/purchaseRecord")
    public ResponseEntity<Map<String, Object>> purchaseRecordGet(
            Page page,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date from,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date to,
            String staffName
    ) {
        return ResponseEntity.ok(buildPageResult(purchaseRecordService.getPurchaseRecordWithPage(from, to, staffName, page)));
    }

    @GetMapping("/purchaseItem")
    public ResponseEntity<List<Map<String, Object>>> purchaseItemGet(Integer purchaseRecordId) {
        return ResponseEntity.ok(purchaseItemService.getPurchaseItemsByPurRecId(purchaseRecordId));
    }
}
